package com.emep.changzhi.analyse.service.information;


import com.alibaba.fastjson.JSONObject;
import com.emep.changzhi.analyse.entity.information.SysSendeRule;
import com.emep.changzhi.analyse.entity.information.SysWarningRecord;
import com.emep.changzhi.analyse.entity.information.SysWarningRule;
import com.emep.changzhi.analyse.model.RabbitMQBean;
import com.emep.changzhi.analyse.repository.basics.SysEmployeeRepository;
import com.emep.changzhi.analyse.repository.information.SysSendRuleRepository;
import com.emep.changzhi.analyse.repository.information.SysWaringRuleRepository;
import com.emep.changzhi.analyse.repository.information.SysWarningRecordRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import com.emep.changzhi.analyse.utils.Regulation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.MapContext;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class SysWaringRuleService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Autowired
    SysWaringRuleRepository repository;

    @Autowired
    SysSendRuleRepository sendRuleRepository;

    @Autowired
    SysEmployeeRepository personRepository;

    //报警记录
    @Autowired
    SysWarningRecordRepository recordRepository;

    public SysWarningRule addWaringRule(Map<String, Object> payload) {

        SysWarningRule sysWarningRule = null;
        try {
            SysWarningRule sysWarningRule1 = (SysWarningRule) MapUtils.convertMap(SysWarningRule.class,payload);
            sysWarningRule = repository.save(sysWarningRule1);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return sysWarningRule;
    }

    public List<SysWarningRule> getList() {

        List<SysWarningRule> SysPromptInfos = null;
        try {
            SysPromptInfos =  repository.findAll();
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysPromptInfos;
    }




    public boolean delete(String id) {

        Boolean result = false;
        try {
             repository.deleteById(id);
           result =true;
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return result;

    }

    //根据获取报警规则

    public String getRule(String warningRuleName,String parameter,String source){



         List<SysWarningRule> warningRules=  repository.findByWarningRuleName(warningRuleName);

          if(warningRules !=null) {

              for (SysWarningRule warningRule : warningRules) {
                  String template=null;
                  Regulation reg = new Regulation();//"speed>1.7||speed<4.2"
                  reg.createExpression(warningRule.getWarningRule());

                  Expression exp = reg.getExpression();
                  JexlContext context = new MapContext();
                  context.set(warningRuleName, parameter);
                  Boolean flag = (Boolean) exp.evaluate(context);
                  //需要报警
                  if (flag) {
                      //rabbitmq
                      template=warningRule.getTemplate();
                      CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
                      RabbitMQBean rabbitMQBean =new RabbitMQBean(source,System.currentTimeMillis(),template);
                      rabbitTemplate.convertAndSend("topicExchange", source,JSONObject.toJSON(rabbitMQBean), correlationData);

                    //weixin
                      int sendtype=warningRule.getSendType();
                      if(sendtype != 0){
                          SysSendeRule sendeRule=sendRuleRepository.getOne(sendtype);

                          switch (sendeRule.getSendRule()){
                              case "微信":
                                  String openid=personRepository.findById(warningRule.getReceiver()).getWeixin();
                                  System.out.println(openid);
                                  //todo
                                  break;
                              case "短信":
                                  String phone=personRepository.findById(warningRule.getReceiver()).getPhone();
                                  System.out.println(phone);
                                  //todo
                                  break;
                          }

                      }

                      //record
                      SysWarningRecord record=new SysWarningRecord();
                      record.setReceiver(warningRule.getReceiver());
                      record.setSendRule(warningRule.getSendType());
                      record.setWarningInfo(template);
                      record.setWarningRule(warningRule.getId());

                      recordRepository.save(record);
                  }

              }
          }

        return  null;
    }

}
